package cache

import (
	"ShortLink/Core/LogHelper"
	redis "ShortLink/Core/Redis"
	"strings"
	"time"
)

// 设置短链缓存
// segment：短链标识
// link：跳转地址
// exp：过期时间
func SetLink(segment string, link string, exp time.Duration) {
	key := strings.Join([]string{redis.Prefix, "_", segment}, "")
	r := redis.Rdb.Set(key, link, exp)
	log := new(LogHelper.LogInfo)
	log.Data = r
	log.Msg = key
	log.Title = "设置短链缓存"
	LogHelper.Info(log)
}

// 获取短链内容
// segment:短链标识
func GetLink(segment string) string {
	key := strings.Join([]string{redis.Prefix, "_", segment}, "")
	val := redis.Rdb.Get(key)
	log := new(LogHelper.LogInfo)
	log.Data = val
	log.Msg = key
	log.Title = "获取短链缓存"
	LogHelper.Info(log)
	return val.Val()
}

// 删除短链
// segment:短链标识
func DelLink(segment string) {
	key := strings.Join([]string{redis.Prefix, "_", segment}, "")
	val := redis.Rdb.Del(key)
	log := new(LogHelper.LogInfo)
	log.Data = val
	log.Msg = key
	log.Title = "删除短链缓存"
	LogHelper.Info(log)
}
